package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 分页查询；包括根据订单号精确查询和根据订单起始时间范围查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime 订单开始时间
     * @param endTime 订单完成时间
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime) throws ParseException {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(number != null, Orders::getNumber, number);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        Date date1 = null;
        //因为从前端传输的是JSON数据(字符串)；所以需要将字符串转为Date类型来作为条件进行SQL查询
        if (beginTime != null && endTime != null) {
            date = simpleDateFormat.parse(beginTime);
            date1 = simpleDateFormat.parse(endTime);
        }
        queryWrapper.ge(date != null, Orders::getOrderTime, date);
        queryWrapper.le(date1 != null, Orders::getCheckoutTime, date1);
        //分页查询
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 派送订单；改变订单状态
     * @orders 数据被封装到了实体类中；MP自动完成该操作
     * @return 未使用到msg属性
     */
    @PutMapping
    public R<String> change(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        //通过id修改状态
        updateWrapper.eq(Orders::getId, orders.getId());
        updateWrapper.set(Orders::getStatus, orders.getStatus());
        //使用修改方法
        orderService.update(updateWrapper);
        return R.success("状态修改成功");
    }

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")

    public R<String> submit(@RequestBody Orders orders) {
        log.info("orders: " + orders);
        orderService.submit(orders);
        return R.success("成功~");
    }

    /**
     * 结算完后，查看订单；分页查询
     * @param page 1
     * @param pageSize 5
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> list(int page, int pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        orderService.page(pageInfo);

        return R.success(pageInfo);
    }
}
